<template>
  <el-tab-pane label="企业人员" name="企业人员">
    <el-form
      label-width="120px"
      label-position="left"
      ref="enterpriseFormRef"
      class="register-form"
    >
      <el-form-item label="账号">
        <el-input v-model="enterpriseForm.account" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="enterpriseForm.phoneNumber" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item class="form-item-code" label="验证码">
        <el-input v-model="enterpriseForm.verificationCode" placeholder="请输入验证码" />
        <el-button @click="getVerificationCode">获取验证码</el-button>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="enterpriseForm.password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input v-model="enterpriseForm.confirmPassword" placeholder="请再次输入密码" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitEnterpriseForm">注册</el-button>
      </el-form-item>
    </el-form>
  </el-tab-pane>
</template>

<script setup lang="ts">
import { ElMessage, type FormInstance } from 'element-plus';
import { ref } from 'vue';
import { register } from '@/api/login';

const enterpriseFormRef = ref<FormInstance>();
// 企业人员表单
const enterpriseForm = ref({
  account: '',
  phoneNumber: '',
  verificationCode: '',
  password: '',
  confirmPassword: '',
});
// 提交企业人员表单
function submitEnterpriseForm() {
  const form = enterpriseFormRef.value;
  form?.validate((valid: boolean) => {
    if (valid) {
      register(enterpriseForm.value).then((res: any) => {
        if (res.code === 200) {
          ElMessage({
            message: '注册成功',
            type: 'success',
          });
        }
      });
    } else {
      return false;
    }
  });
}
// 触发获取验证码事件
const emit = defineEmits(['getCode']);
function getVerificationCode() {
  emit('getCode', enterpriseForm.value.phoneNumber);
}
</script>

<style scoped>
.form-item-code :deep(.el-form-item__content) {
  flex-wrap: nowrap;
}
</style>
